<?php
namespace app\index\controller;

//继承
use think\Controller;

use think\Session;
use think\Request;
//extend中的所有 第三方应用都需要使用 根命名空间 \ 
use \sina\SaeTOAuthV2;	
use \sina\SaeTClientV2;


class Sina extends Controller
{
	
	public function foot() 
	{

		// return $this->fetch();
        // return 'a';
//                                $path = STATIC_HOME;
//                                $this->assign(['path' => $path]);
		// $this->display();
//		return $this->fetch();
		// dump(1);
	}
	
	
    /*public function index()
    {
        return '这是商城项目的首页';
    }*/




   //--------微博第三方登录接口SDK测试

    //获取跳转到微博的url,
    public function sina()
    {
        var_dump(WB_AKEY);
        var_dump(WB_SKEY);
        die;
    	//实例化auth类
        $o = new SaeTOAuthV2(WB_AKEY,WB_SKEY);

        //获取跳转到微博的url
        $code_url = $o->getAuthorizeURL(WB_CALLBACK_URL);

       	//传递参数,渲染页面
        $this->assign('code_url',$code_url);
        //dump(APP_PATH);
        return $this->fetch();

    }

    //获取跳转到微博的回调url,
    public function callback()
    {
    		//实例化auth类
	      $o = new SaeTOAuthV2(WB_AKEY,WB_SKEY);
	      $keys = array();

	      //接收传值
	      $param = Request::instance()->param();
	      //dump($param);

	      //获取url,token和code的三者的值
	      $keys['code'] = $param['code'];
	      $keys['redirect_uri'] = WB_CALLBACK_URL;
	      $token = $o->getAccessToken('code',$keys);

	      //若口令token存在,存入session , 否则返回提示信息
	      if ($token) {

	      	//授权成功 , 将token保存到session中
	      	Session::set('token',$token);
	      	return $this->fetch('callback');
	      } else {
	      	//授权失败
	      	return $this->errors('授权失败');
	      }
    }

    //微博信息显示页面
    public function weibolist()
    {
    	//实例化类
    	$o = new SaeTClientV2(WB_AKEY,WB_SKEY,Session::get('token')['access_token']);

    	//或 ms信息, uid , 用户信息
    	$ms = $o->home_timeline();
    	$uid_get = $o->get_uid();
    	$uid = $uid_get['uid'];
    	$user_message = $o->show_user_by_id($uid);

	//传递参数,渲染页面
    	$this->assign('ms',$ms);
    	$this->assign('uid' , $uid);
    	return $this->fetch();

    }

    //发微博
    public function send()
    {
    	//实例化类
    	$o = new SaeTClientV2(WB_AKEY,WB_SKEY,Session::get('token')['access_token']);	
    	//获取text的值
    	$res = $o->update(Request::instance()->param()['text']);

    	//通过判断错误号,判断微博是否发送成功
    	if (isset($res['error_code']) && $res['error_code'] > 0) {

    		return $this->error('发送失败');
    	} else {

    		//发送成功,获取用户相关细信息
    		$ms = $o->home_timeline();
	    	$uid_get = $o->get_uid();
	    	$uid = $uid_get['uid'];
	    	$user_message = $o->show_user_by_id($uid);

	    	//传递参数,渲染页面
	    	$this->assign('ms',$ms);
	    	$this->assign('uid' , $uid);
	    	return $this->fetch('weibolist');	//注意

    	}

    }



}
